<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Fishinggroundcollects extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\FishingGroundCollects();
    }

    /**
     * 收藏的钓场列表
     */
    public function lists()
    {

        $pageSize = input('pagesize', 15);

        $userId = $this->auth->id;
        $where = [
            'user_id' => $userId,
        ];

        $order = input('order', 'createtime');
        $sort = input('sort', 'desc');

        $list = self::$model->where($where)->order($order . ' ' . $sort)->paginate($pageSize);
        if ($list) {
            $fish_types_arr = config('site.fish_types');
            foreach ($list as $val) {
                $groundInfo = \app\common\model\FishingGround::field('id,name,address')->find($val->ground_id);

                $val->name = $groundInfo->name;
                $val->address = $groundInfo->address;

                $ids = \app\common\model\FishingGroundFishs::where('ground_id', $val->ground_id)->group('type')->column('type');
                $fishs_arr = [];
                foreach ($ids as $vv) {
                    $fishs_arr[] = $fish_types_arr[$vv];
                }
                $val->fishs_arr = $fishs_arr;
            }
        }
        $this->success('收藏的钓场列表', $list);
    }

    /**
     * 钓场收藏
     */
    public function create()
    {
        $groundId = input('ground_id');
        if (!$groundId) {
            $this->error('参数缺失！');
        }

        $where = [
            'user_id' => $this->auth->id,
            'ground_id' => $groundId,
        ];

        $row_info = self::$model->where($where)->find();
        if (!$row_info) {
            $gdata = [
                'user_id' => $this->auth->id,
                'ground_id' => $groundId,
            ];

            $res = self::$model->create($gdata);
            if (!$res) {
                $this->error('收藏失败！');
            }
            $this->success('收藏成功！');
        } else {
            $res = self::$model->where('id', $row_info->id)->delete();
            if (!$res) {
                $this->error('取消收藏失败！');
            }
            $this->success('取消收藏成功！');
        }
    }
}
